# This test shows the asynchronous use of an mq server
import time
import os
import random
from mediadart.mqueue.mqclient_async import Proxy

responses = 0
# home many requests to lauch
NUMREQUESTS=15

def print_cb(status_ok, result, n):
    global responses, NUMREQUESTS
    responses += 1
    if status_ok:
        print 'OK: %s = %s'  % (n, result)
    else:
        print 'ERROR: %s: error %s' % (n, str(result))
    if responses >= NUMREQUESTS:
        t1 = time.time()
        print 'elapsed = %.3f' % (t1-t0)


print 'Process %s' % os.getpid()
t0 = time.time()
for i in xrange(NUMREQUESTS):
    label = 'request-%d' % (i) 
    seconds = random.randint(5, 15)
    print 'requesting createfile %s %d' % (label, seconds)
    Proxy('DoStuff', print_cb, label).go(label, seconds)
